<template>
  <div class="page">
    <el-form :model="searchForm" label-position="left" inline label-suffix="：" size="small">
      <div class="flex wap flex-start">
        <el-form-item label="支出事由">
          <el-input v-model="searchForm.costinfo" clearable></el-input>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker v-model="dataTime" type="daterange" range-separator="~" start-placeholder="开始日期"
            end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" @click="getData">查询</el-button>
        </el-form-item>
      </div>
    </el-form>
    <div class="flex wap flex-start" style="padding: 0px 0 10px 0">
      <el-button type="success" @click="addData">新增</el-button>
      <div style="width:20px"></div>
      <!-- <el-button type="danger" @click="del">删除</el-button> -->
    </div>
    <el-table :data="tableData" border style="width: 100%"  height="600"  :header-cell-style="{
        background: '#eef1f6',
        color: '#606266',
        'text-align': 'center'
      }"
      :cell-style="{ 'text-align': 'center' }">
      <!-- <el-table-column type="selection"></el-table-column> -->

      <!-- <el-table-column prop="costid" label="流水号"></el-table-column> -->
      <el-table-column prop="costmoney" label="支出金额"></el-table-column>
      <el-table-column prop="costtime" label="支出时间"></el-table-column>
      <el-table-column prop="costinfo" label="支出事由" show-overflow-tooltip></el-table-column>
      <el-table-column prop="whopay" label="付款信息"></el-table-column>
      <el-table-column prop="createtime" label="记账时间"></el-table-column>
      <el-table-column prop="costimg" label="付款凭证图片">
        <template slot-scope="scop">
          <el-image fit="cover" style="width: 80px; height: 50px" :src="$Url + '/upload/' + scop.row.costimg"
            :preview-src-list="[$Url + '/upload/' + scop.row.costimg]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="" label="操作" width="120">
        <template slot-scope="scop">
          <!-- <el-button type="primary" icon="el-icon-edit" circle></el-button> -->
          <el-button type="danger" icon="el-icon-delete" circle @click="del(scop.row.costid)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="float:right;padding:10px">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 20, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog title="新增支出信息" center :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="addForm" ref="addSetForm" :rules="rule" label-width="100px" class="edit-form" label-suffix="：">
        <el-form-item label="支出事由" prop="costinfo">
          <el-input v-model="addForm.costinfo" placeholder="请输入支出事由" clear></el-input>
        </el-form-item>
        <el-form-item label="支出金额" prop="costmoney">
          <el-input v-model="addForm.costmoney" placeholder="请输入支出金额" clear></el-input>
        </el-form-item>
        <el-form-item label="支出时间" prop="costtime">
          <el-date-picker v-model="addForm.costtime" type="datetime" placeholder="请选择支出时间" value-format="yyyy-MM-dd hh:mm:ss">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="付款信息" prop="whopay">
          <el-input v-model="addForm.whopay" placeholder="请输入付款信息" clear></el-input>
        </el-form-item>
        <el-form-item label="支付凭证">
          <upload v-model="addForm.costimg" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSave">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { findCost, addCost, delCost } from "@/api/user";
import upload from "@/components/uploadFile";
export default {
  components: {
    upload
  },
  data() {
    return {
      total: 0,
      pageNum: 1,
      pageSize: 10,
      tableData: [],
      dataTime: [],
      searchForm: {
        costinfo: "",
        begintime: "",
        endtime: ''
      },
      dialogFormVisible: false,
      addForm: {
        costinfo: '',
        costmoney: '',
        costtime: '',
        whopay: '',
        costimg: '',
      },
      rule: {
        costinfo: [
          { required: true, message: '请填写支出信息', trigger: 'change' }
        ],
        costmoney: [
          { required: true, message: '请填写支出金额', trigger: 'change' }
        ],
        costtime: [
          { required: true, message: '请选择支出时间', trigger: 'change' }
        ],

      },
    };
  },
  watch: {
    dataTime(n) {
      console.log(n);
      if (n) {
        this.searchForm.begintime = n[0]
        this.searchForm.endtime = n[1]
      } else {
        this.searchForm.begintime = ''
        this.searchForm.endtime = ''
      }
    }
  },
  computed: {
    queryData() {
      const {costinfo, begintime, endtime} = this.searchForm
      return {
        costinfo,
        begintime,
        endtime,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    async getData() {
      const res = await findCost.post(this.queryData);
      if (res.resultCode === "000000") {
        this.tableData = res.data.costList;
        this.total = res.data.count;
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    addData() {
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['addSetForm'].resetFields();
      })
      this.addForm = {
        costinfo: '',
        costmoney: '',
        costtime: '',
        whopay: '',
        costimg: '',
      }
    },
    addSave() {
      this.$refs.addSetForm.validate(async (valid) => {
        if (valid) {
          const res = await addCost.post(this.addForm);
          if (res.resultCode === "000000") {
            this.$message.success(res.resultInfo);
            this.dialogFormVisible = false
            this.getData()
          } else {
            this.$message.error(res.resultInfo);
          }
        } else {
          return false;
        }
      });
    },
    del(id) {
      this.$confirm("此操作将删除该项目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.delItem(id)
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async delItem(id) {
      const res = await delCost.post({ costid: id });
      if (res.resultCode === "000000") {
        this.$message.success(res.resultInfo);
        this.getData()
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    handleSizeChange(val) {
      this.pageNum = 1;
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getData();
    }
  }
};
</script>
<style lang="less" scoped></style>
